//
//  TSPictureFlowLayout.swift
//  Picture
//
//  Created by 王磊 on 28/04/2017.
//  Copyright © 2017 ThreeStone. All rights reserved.
//

import UIKit

class TSPictureFlowLayout: UICollectionViewFlowLayout {
    
    override init() {
        super.init()
        
        minimumLineSpacing = Picture_Item_Min_Space
        
        scrollDirection = .Horizontal
        
        itemSize = CGSizeMake(Picture_Item_Width, Picture_Item_Height)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
extension TSPictureFlowLayout {
    
    override func prepareLayout() {
        
        sectionInset = UIEdgeInsetsMake(0, Margin, 0, Margin)
    }
}

extension TSPictureFlowLayout {
    
    override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
        
        return true
    }
}
extension TSPictureFlowLayout {
    
    override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        
        guard let items = super.layoutAttributesForElementsInRect(rect) else {
            
            return nil
        }
        
        guard let collectionView = collectionView else {
            
            return nil
        }
        
        let horizontalCenterX = collectionView.contentOffset.x + CGRectGetWidth(collectionView.bounds) / 2
        
        var mutable: Array<UICollectionViewLayoutAttributes> = [UICollectionViewLayoutAttributes]()
        
        _ = items.map {
            
            if abs(Int32($0.center.x - horizontalCenterX)) < Int32($0.size.width / 2) {
                
                let scale = 1 + (1 - abs($0.center.x - horizontalCenterX) / ($0.size.width / 2)) / 4
                
                $0.zIndex = 5
                
                $0.transform3D = CATransform3DMakeScale(scale, scale, 1)
                
            }
            mutable += [$0]
        }
        
        return mutable
    }
    
    
    override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
        return super.layoutAttributesForItemAtIndexPath(indexPath)
    }
    
    override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
        
        var offsetAdjustment: CGFloat = 0
        
        let horizontalCenterX = proposedContentOffset.x + CGRectGetWidth(collectionView!.bounds) / 2
        
        guard let collectionView = collectionView else {
            
            return CGPointZero
        }
        
        let targetRect = CGRectMake(proposedContentOffset.x, proposedContentOffset.y, CGRectGetWidth(collectionView.bounds), CGRectGetHeight(collectionView.bounds))
        
        guard let items = super.layoutAttributesForElementsInRect(targetRect) else {
            
            return CGPointZero
        }
        
        var tmpAttr: UICollectionViewLayoutAttributes!
        
        _ = items.map({
            
            if abs($0.center.x - horizontalCenterX) <= $0.size.width / 2 {
                
                offsetAdjustment = $0.center.x - horizontalCenterX
                
                return
            }
            
            //没有则比较全部，找出最近
            
            if abs($0.center.x - horizontalCenterX) <= abs(tmpAttr != nil ? tmpAttr.center.x - horizontalCenterX : horizontalCenterX ) {
                
                tmpAttr = $0
                
                offsetAdjustment = $0.center.x - horizontalCenterX;
            }
        })
        
        return CGPointMake(proposedContentOffset.x + offsetAdjustment, proposedContentOffset.y)
    }
    
}